package com.travelDevelop.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.travelDevelop.commons.bean.User;
import com.travelDevelop.commons.bean.UserExample;
import com.travelDevelop.commons.util.StringUtil;
import com.travelDevelop.commons.util.WebConst;
import com.travelDevelop.dao.UserMapper;

@Service
public class UserService {

	@Autowired
	public UserMapper userMapper;

	/**
	 * 查询用户
	 * @param userName 用户名
	 * @param password 密码
	 * @return
	 */
	public User getUser(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}
	
	public User getUserByUsernameAndPwd(String username,String password){
		if(StringUtil.isNull(username)||StringUtil.isNull(password)){
			return null;
		}
		User user = new User();
		//判断是否为电话号码格式
		if (Pattern.matches("\\d{11}", username)) {
			//users表mobile字段与password字段查新该用户信息
			//user = userMapper.selectMobileByPss(username, password);
			user.setPhone(username);
			System.out.println("电话");
		}else if (Pattern.matches(WebConst.emailRegex, username)) {
			user.setEmail(username);
            System.out.println("邮箱");
        } else {
        	user.setUsername(username);
        	 System.out.println("正常用户名");
        }
		user.setPassword(password);
		
		return userMapper.selectMobileByPss(user);
	}
	
	
	public List<User> getUsers(UserExample ue){
		ue.setOrderByClause(" create_time desc ");
		return userMapper.selectByExample(ue);
	}
	
	public long countUsers(UserExample ue){
		return userMapper.countByExample(ue);
	}
	
	public long isUserExists(User ue){
		return userMapper.isUserExists(ue);
	}
	
	
	public void deleteUser(String userId){
		userMapper.deleteByPrimaryKey(userId);
	}
	
	public List<User> getUsersNotinPartyMember(UserExample ue){
		ue.setOrderByClause(" create_time desc ");
		return userMapper.selectByExampleNotinPartyMember(ue);
	}
	
	public long countUsersNotinPartyMember(UserExample ue){
		return userMapper.countByExampleNotinPartyMember(ue);
	}
}
